﻿using DataAccess.DB;
using DuocThaiBinh.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DuocThaiBinh.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Session["CuaHangID"] == null || Convert.ToInt32(Session["CuaHangID"]) == 0)
            {
                Response.Redirect("/", true);
            }
            base.OnActionExecuting(filterContext);
        }

        protected db_YTeTBEntities db = new db_YTeTBEntities();
        protected UserProfile GetCurrentUser()
        {
            var currentUser = db.UserProfiles.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
            if (currentUser == null) throw new CurrentUserIsNull();
            return currentUser;
        }

        protected CuaHang GetCurrentCuaHang()
        {
            var currentCuaHang = db.CuaHangs.Find(Convert.ToInt32(Session["CuaHangID"]));
            if (currentCuaHang == null) throw new CurrentCuaHangIsNull();
            return currentCuaHang;
        }

        protected int GetCurrentCuaHangID()
        {
            return Convert.ToInt32(Session["CuaHangID"]);
        }
    }
}